home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 2 / Meeting Pearls Vol. II (1995)(GTI - Schatztruhe)[!].iso / Pearls / dev / GUI / GUIFront / Demos / LocalizeTest / LocalizeTest.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  7KB  |  254 lines

  1.  
  2. /* Locale.c - Localizing example
  3.  *
  4.  * This demo illustrates the basic concepts of GUIFront localizing. It opens
  5.  * up a GUI exactly like the "FramePrefs" example in the parent directory,
  6.  * but localized this time.
  7.  * This is the basic GUI setup code. It is not much different from the
  8.  * 'Generic.c' file in the parent directory, only it contains extra code to
  9.  * open locale.library, a routine to fetch localized strings, and the localizer
  10.  * hook code used by GUIFront.
  11.  */
  12.  
  13. /* Include everything */
  14.  
  15. #include <libraries/guifront.h>
  16. #include <proto/guifront.h>
  17. #include <proto/dos.h>
  18. #include <proto/exec.h>
  19. #include <proto/locale.h>
  20.  
  21. #define CATCOMP_ARRAY
  22. #include "strings.h"
  23.  
  24. /* Library bases */
  25.  
  26. struct Library *GUIFrontBase;
  27. struct Library *LocaleBase;
  28.  
  29. /* Global data */
  30.  
  31. struct Catalog *catalog;
  32.  
  33. /* Imported from the demo module */
  34.  
  35. extern STRPTR DEMO_Version,
  36.               DEMO_LongDesc,
  37.               DEMO_Author,
  38.               DEMO_Date;
  39.  
  40. extern BOOL   DEMO_Backfill;
  41.  
  42. extern STRPTR DEMO_AppID;
  43.  
  44. extern DEMO_InitialOrientation;
  45. extern ULONG DEMO_LayoutList[];
  46. extern GadgetSpec *DEMO_GadgetSpecList[];
  47.  
  48. extern struct NewMenu DEMO_NewMenu[];
  49.  
  50. /* Function prototypes */
  51.  
  52. static GUIFront *buildgui(ExtErrorData * const, GUIFrontApp * const guiapp, short const left, short const top);
  53.  
  54. /* Code */
  55.  
  56. /* First, a tiny function which returns catalog strings */
  57.  
  58. STRPTR cat(const int num)
  59. {
  60.     short i;
  61.  
  62.     for (i = 0; CatCompArray[i].cca_ID != num; i++)
  63.         ;
  64.  
  65.     if (LocaleBase)
  66.         return((char *)GetCatalogStr(catalog,num,CatCompArray[i].cca_Str));
  67.     else
  68.         return(CatCompArray[i].cca_Str);
  69. }
  70.  
  71. /* This is the localizer hook used by (called by) GUIFront */
  72.  
  73. __saveds __asm STRPTR localizerfunc(register __a0 struct Hook * hook,
  74.                                     register __a2 GUIFront *gui,
  75.                                     register __a1 LocaleHookMsg *lhm)
  76. {
  77.     /* What type of data does GUIFront want us to localize? */
  78.  
  79.     switch (lhm->lhm_Kind)
  80.     {
  81.         case LHMK_StringID:
  82.  
  83.             /* A simple string */
  84.  
  85.             return (cat(lhm->lhm_Data.lhmd_StringID));
  86.  
  87.         case LHMK_GadgetSpec:
  88.         {
  89.             /* A GadgetSpec (the gadget label). We grab the catalog string
  90.              * id we stored in the GadgetText field of the GadgetSpec,
  91.              * and return the corresponding catalog string.
  92.              */
  93.  
  94.             ULONG str_id = (ULONG)lhm->lhm_Data.lhmd_GadgetSpec->gs_ng.ng_GadgetText;
  95.  
  96.             /* We don't want to localize gadgets which don't have labels */
  97.  
  98.             if (str_id > 0)
  99.                 return (cat(str_id));
  100.  
  101.             return(NULL);
  102.         }
  103.  
  104.         case LHMK_NewMenu:
  105.         {
  106.             /* A newmenu entry. We take the catalog string id we stored
  107.              * in the nm_Label field of the NewMenu entry and return
  108.              * the corresponding catalog string
  109.              */
  110.  
  111.             struct NewMenu *nm = lhm->lhm_Data.lhmd_NewMenu;
  112.  
  113.             /* We take care not to attempt localizing something we
  114.              * shouldn't. A NULL return tells GUIFRont we can't localize
  115.              * this label.
  116.              */
  117.  
  118.             if ((nm->nm_Type != NM_END) && (nm->nm_Label != NM_BARLABEL))
  119.                 return (cat((ULONG)nm->nm_Label));
  120.  
  121.             return (NULL);
  122.         }
  123.     }
  124. }
  125.  
  126. /* Trivial hook interface to the above routine */
  127.  
  128. struct Hook localizerhook =
  129. {
  130.     {NULL, NULL},
  131.     (ULONG (*)())localizerfunc,
  132.     NULL, NULL
  133. };
  134.  
  135. main()
  136. {
  137.     /* Attempt to open library */
  138.  
  139.     if (GUIFrontBase = OpenLibrary(GUIFRONTNAME, GUIFRONTVERSION))
  140.     {
  141.         GUIFrontApp *guiapp;
  142.  
  143.         /* Attempt to open a catalog */
  144.  
  145.         if (LocaleBase = OpenLibrary("locale.library",0))
  146.             catalog = OpenCatalogA(NULL,"localizetest.catalog",NULL);
  147.  
  148.         /* Create our application anchor structure */
  149.  
  150.         if (guiapp = GF_CreateGUIApp(DEMO_AppID,
  151.             GFA_Version,    DEMO_Version,
  152.             GFA_LongDesc,   DEMO_LongDesc,
  153.             GFA_Author,     DEMO_Author,
  154.             GFA_Date,       DEMO_Date,
  155.             GFA_VisualUpdateSigBit, SIGBREAKB_CTRL_F, /* For simplicity */
  156.             TAG_DONE))
  157.         {
  158.             GUIFront *gui;
  159.             ExtErrorData exterr;
  160.             short left = -1, top = -1;
  161.  
  162.             /* Create a gui for our application */
  163.  
  164. creategui:  if (gui = buildgui(guiapp,&exterr,left,top))
  165.             {
  166.                 BOOL done = FALSE;
  167.  
  168.                 /* Process input events */
  169.  
  170.                 while (!done)
  171.                 {
  172.                     struct IntuiMessage *imsg;
  173.                     ULONG signals;
  174.  
  175.                     /* Wait for an event to occur */
  176.  
  177.                     signals = GF_Wait(guiapp,SIGBREAKF_CTRL_F);
  178.  
  179.                     if (signals & SIGBREAKF_CTRL_F) /* Update visuals? */
  180.                     {
  181.                         /* Extract current left & topedge of our GUI
  182.                          * window, so we can open it at the same
  183.                          * location.
  184.                          */
  185.  
  186.                         GF_GetGUIAttr(gui, GUI_LeftEdge, &left,
  187.                                            GUI_TopEdge,  &top,
  188.                                            TAG_DONE);
  189.  
  190.                         GF_DestroyGUI(gui);
  191.                         goto creategui;
  192.                     }
  193.  
  194.                     /* We only bother to listen for CLOSEWINDOW events.
  195.                      * Of course, in a real application, you would be
  196.                      * examining the Class field for IDCMP_GADGETUP
  197.                      * messages and act accordingly.
  198.                      */
  199.  
  200.                     while (imsg = GF_GetIMsg(guiapp))
  201.                     {
  202.                         if (imsg->Class == IDCMP_CLOSEWINDOW)
  203.                             done = TRUE;
  204.  
  205.                         GF_ReplyIMsg(imsg);
  206.                     }
  207.                 }
  208.  
  209.                 /* We're done with the GUI, so free it. GF_DestroyGUIApp
  210.                  * actually does this for us, but it still looks nicer if
  211.                  * we do it manually (I think :-)
  212.                  */
  213.  
  214.                 GF_DestroyGUI(gui);
  215.             }
  216.             else
  217.                 Printf(cat(MSG_CantCreateGUI), exterr.ee_ErrorCode, exterr.ee_ErrorData);
  218.  
  219.             /* Destroy application anchor strucuture */
  220.  
  221.             GF_DestroyGUIApp(guiapp);
  222.         }
  223.         else
  224.             PutStr(cat(MSG_CantCreateApplication));
  225.  
  226.         if (LocaleBase)
  227.         {
  228.             if (catalog) CloseCatalog(catalog);
  229.             CloseLibrary(LocaleBase);
  230.         }
  231.  
  232.         CloseLibrary(GUIFrontBase);
  233.     }
  234.     else
  235.         PutStr("Requires guifront.library V37+\n");
  236. }
  237.  
  238. /* (Re)create a gui for our application */
  239.  
  240. static GUIFront *buildgui(GUIFrontApp * const guiapp, ExtErrorData * const exterr, short const left, short const top)
  241. {
  242.     return (GF_CreateGUI(guiapp, DEMO_LayoutList,DEMO_GadgetSpecList,
  243.         GUI_InitialOrientation, DEMO_InitialOrientation,
  244.         GUI_Backfill,           DEMO_Backfill,
  245.         GUI_ExtendedError,      exterr,
  246.         GUI_WindowTitle,        cat(MSG_WindowTitle),
  247.         GUI_OpenGUI,            TRUE,
  248.         GUI_LocaleFunc,         &localizerhook,
  249.         GUI_NewMenuLoc,         DEMO_NewMenu,
  250.         GUI_LeftEdge,            left,
  251.         GUI_TopEdge,            top,
  252.         TAG_DONE));
  253. }
  254.